package com.jcsz.gulimall.order.listener;

import com.jcsz.gulimall.order.entity.OrderEntity;
import com.jcsz.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @className: OrderCloseListener
 * @author: 一把Po吉他
 * @date: 2022/11/16 13:27
 * @version: 1.0
 **/
@Component
@RabbitListener(queues = "order.release.queue")
public class OrderCloseListener {
    @Autowired
    OrderService orderService;

    @RabbitHandler
    public void orderClose(OrderEntity orderEntity, Message message, Channel channel) throws IOException {
        System.out.println("收到订单关闭消息..." + orderEntity.getOrderSn());
        try {
            orderService.orderClose(orderEntity);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
        }

    }
}
